package org.jetlinks.pro;

import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.core.param.QueryParam;
import org.hswebframework.web.api.crud.entity.EntityFactoryHolder;
import org.hswebframework.web.api.crud.entity.PagerResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用游标分页的查询结果,如果返回此结果.则在动态查询时,需要指定上一次查询返回的scrollId
 *
 * <pre>
 *     {
 *         "terms":[],
 *         "context":{
 *             "scrollId":"查询结果返回的scrollId,不传入则认为是第一页"
 *         }
 *     }
 * </pre>
 *
 * @param <E>
 * @see org.jetlinks.pro.utils.ScrollPagingUtils
 */
@Getter
@Setter
public class ScrollPagerResult<E> extends PagerResult<E> {
    private boolean scroll = true;

    private String scrollId;

    @SuppressWarnings("all")
    public static <E> ScrollPagerResult<E> of(int total, List<E> list, String scrollId, QueryParam queryParam) {
        ScrollPagerResult<E> result;
        result = EntityFactoryHolder.newInstance(ScrollPagerResult.class, ScrollPagerResult::new);
        result.scrollId = scrollId;
        result.setTotal(total);
        result.setData(list);
        result.setPageSize(queryParam.getPageSize());
        result.setPageIndex(queryParam.getPageIndex());
        return result;
    }

    public static <E> ScrollPagerResult<E> empty(QueryParam queryParam) {
        return ScrollPagerResult.of(0, new ArrayList<>(), null, queryParam);
    }
}
